package initialize

import (
	"errors"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"week1zk-project/user-rpc/global"
	"week1zk-project/user-rpc/internal/model"
)

//初始化mysql

func InitMysql() {
	var err error
	mysqlConfig := global.AppData.Service.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysqlConfig.User, mysqlConfig.Password, mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.Database)
	global.Once.Do(func() {
		global.DB, err = gorm.Open(mysql.Open(dsn))
		if err != nil {
			panic(errors.New("mysql数据库连接失败"))
			return
		}
		fmt.Println("mysql数据库连接成功")
		global.DB.AutoMigrate(new(model.User))
	})

}
